#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>

struct pollfd fds[100]; 
nfds_t nfds = 0;

static void fds_init(void)
{
	int i;
	
	for(i=0; i<sizeof(fds)/sizeof(fds[0]); i++)
	{
		//将描述符设置为无效值
		fds[i].fd = -1;
	}
}

static int fds_add(int fd, short events)
{
	int i;
	
	for(i=0; i<sizeof(fds)/sizeof(fds[0]); i++)
	{
		if(fds[i].fd < 0)
		{
			fds[i].fd = fd;
			fds[i].events = events;
			if((i+1) > nfds)
				nfds = i+1;
			printf("add fd %d, nfds %ld\r\n", fd, nfds);
			return 0;
		}
	}
	return -1;
}

static int fds_delete(int fd)
{
	int i;
	
	for(i=0; i<sizeof(fds)/sizeof(fds[0]); i++)
	{
		if(fds[i].fd == fd)
		{
			close(fds[i].fd);
			fds[i].fd = -1;
			printf("delete fd %d, nfds %ld\r\n", fd, nfds);
			return 0;
		}
	}
	return -1;
}

static void fds_clear(void)
{
	int i;
	
	for(i=0; i<sizeof(fds)/sizeof(fds[0]); i++)
	{
		if(fds[i].fd >= 0)
		{
			close(fds[i].fd);
			fds[i].fd  = -1;
		}
	}
}

int main(int argv, char *argc[])
{
	int sock;
	int client;
	int restul;
	nfds_t i;
	socklen_t addr_lenght;
	struct sockaddr_in client_addr;
	struct sockaddr_in server_addr;
	char buffer[100];
	char *ip = "0.0.0.0";
	char *port = "5007";
	int optval = 1;
	
	if(argv > 1)
		ip = argc[1];
	if(argv > 2)
		port = argc[2];
	
	//初始化描述符列表
	fds_init();
	
	//创建套接字
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if(sock == -1)
	{
		perror("socket");
		return -1;
	}
	
	//允许端口重用
	restul = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optval, sizeof(int));
	if(restul == -1)
	{
		close(sock);
		perror("setsockopt");
		return -1;
	}
	
	//绑IP和端口
	memset(&server_addr, 0, sizeof(server_addr));
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(port));
	server_addr.sin_addr.s_addr = inet_addr(ip);
	restul = bind(sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
	if(restul == -1)
	{
		close(sock);
		perror("bind");
		return -1;
	}
	printf("IP %s; port %d\n", inet_ntoa(server_addr.sin_addr), ntohs(server_addr.sin_port));
	
	//监听套接字
	restul = listen(sock, 100);
	if(restul == -1)
	{
		close(sock);
		perror("listen");
		return -1;
	}
	
	//将服务器套接字加入描述符列表
	fds_add(sock, POLLIN);
	
	while(1)
	{
		//通过poll同时监听多个描述符
		restul = poll(fds, nfds, -1);
		if(restul == -1)
		{
			fds_clear();
			perror("poll");
			return -1;
		}
		for(i=0; i<nfds; i++)
		{
			if((fds[i].fd >= 0) && (fds[i].revents && POLLIN))
			{
				if(fds[i].fd == sock)
				{
					//获取监听到的客户端描述符
					addr_lenght = sizeof(client_addr);
					client = accept(sock, (struct sockaddr *)&client_addr, &addr_lenght);
					if(client == -1)
					{
						fds_clear();
						perror("accept");
						return -1;
					}
					
					//将描述符加入描述符列表
					restul = fds_add(client, POLLIN);
					if(restul == -1)
					{
						close(client);
						printf("fds list of overflow\r\n");
					}
				}
				else
				{
					//读取客户端数据
					memset(buffer, 0, sizeof(buffer));
					restul = read(fds[i].fd, buffer, sizeof(buffer)-1);
					//读取出错
					if(restul == -1)
						perror("read");
					//对方关闭连接
					if(restul <= 0)
						fds_delete(fds[i].fd);
					//读取成功
					if(restul > 0)
					{
						//把数据写回客户端
						restul = write(fds[i].fd, buffer, restul);
						if(restul == -1)
						{
							fds_delete(fds[i].fd);
							perror("write");
						}
					}
				}
			}
		}
	}
}
